package com.sikaryofficial.workbench.domain.dto.req.goods;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.UniqueElements;

import javax.validation.Valid;
import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class CreateOrUpdateGoodsReq {

    /**
     * 商品ID（创建时为空）
     */
    @ApiModelProperty(value = "商品ID（创建时为空）")
    private Long goodsId;

    /**
     * 商品名称
     */
    @ApiModelProperty(value = "产品名称")
    @NotEmpty(message = "产品名称不能为空")
    private String goodsName;

    /**
     * 品牌封面图片
     */
    @ApiModelProperty(value = "品牌封面图片")
    private String coverUrl;

    /**
     * 产品图片
     */
    @ApiModelProperty(value = "产品图片链接数组")
    private List<GoodsMaterialReq> goodsMaterials;

    /**
     * 币种
     */
    @ApiModelProperty(value = "币种")
    @NotEmpty(message = "币种不能为空")
    private String currency;

    /**
     * 单价-零售价
     */
    @ApiModelProperty(value = "单价-零售价")
    @NotNull(message = "零售价不能为空")
    private BigDecimal unitPrice;
    /**
     * 单价-零售价-现价
     */
    @ApiModelProperty(value = "单价-零售价-现价")
    private BigDecimal currentUnitPrice;

    /**
     * 批发价格
     */
    @ApiModelProperty(value = "批发价格")
    @NotNull(message = "批发价格不能为空")
    private BigDecimal wholesaleUnitPrice;
    /**
     * 批发价格-现价
     */
    @ApiModelProperty(value = "批发价格-现价")
    private BigDecimal currentWholesaleUnitPrice;

    /**
     * 阶梯价
     */
    @ApiModelProperty(value = "阶梯价")
    @Size(min = 1, message = "阶梯价不能为空")
    private List<@Valid PriceTier> priceTierList;

    /**
     * 口味
     */
    @ApiModelProperty(value = "口味")
    @NotEmpty(message = "口味不能为空")
    private List<@Valid GoodsTaste> goodsTasteList;

    @UniqueElements(message = "口味名称重复[{duplicates}]")
    public List<String> getCheckRepeatResult() {
        return goodsTasteList == null
                ? null
                : goodsTasteList.stream().map(CreateOrUpdateGoodsReq.GoodsTaste::getTasteName).collect(Collectors.toList());
    }

    /**
     * 商品描述
     */
    @ApiModelProperty(value = "商品描述")
    @NotEmpty(message = "描述不能为空")
    private String goodsDesc;
    /**
     * 商品参数
     */
    @ApiModelProperty(value = "商品参数")
    @NotEmpty(message = "参数不能为空")
    private String goodsSpec;


    /**
     * 上下架状态：0-未上架 1-已上架
     * @see com.sikaryofficial.workbench.constant.GoodsListingStatusEnum
     */
    @ApiModelProperty(value = "上下架状态：0-未上架 1-已上架", example = "0")
    @NotNull(message = "上下架状态不能为空")
    private Integer listingStatus;

    /**
     * 型号
     */
    @ApiModelProperty(value = "型号")
    private String model;

    /**
     * 款式: style:disposable 一次性,cartridge 弹药筒,other 其他
     */
    @ApiModelProperty(value = "款式: style:disposable 一次性,cartridge 弹药筒,other 其他", example = "disposable")
    @NotEmpty(message = "款式不能为空")
    private String style;

    /**
     * 品牌编码
     */
    @ApiModelProperty(value = "品牌编码", example = "SKE")
    @NotEmpty(message = "品牌不能为空")
    private String brandNo;

    /**
     * 电池容量，单位： 毫安小时 mAh
     */
    @ApiModelProperty(value = "电池容量，单位： 毫安小时 mAh", example = "600")
    @NotNull(message = "电池容量不能为空")
    private Integer batteryCapacity;

    /**
     * 烟油容量, 单位：毫升 ml
     */
    @ApiModelProperty(value = "烟油容量, 单位：毫升 ml", example = "600")
    @NotNull(message = "烟油容量不能为空")
    private Integer liquidCapacity;

    /**
     * 口吸数
     */
    @ApiModelProperty(value = "口吸数", example = "600")
    @NotNull(message = "口吸数不能为空")
    private Integer puffNum;

    /** 尼古丁含量, 单位：mg/ml */
    @ApiModelProperty(value = "尼古丁含量, 单位：mg/ml", example = "600")
    @NotNull(message = "尼古丁含量不能为空")
    private Integer nicotineCapacity;

    @Data
    public static class PriceTier {
        /**
         * 最小数量
         */
        @ApiModelProperty(value = "最小数量")
        private Integer minQuantity;
        /**
         * 最大数量
         */
        @ApiModelProperty(value = "最大数量")
        private Integer maxQuantity;
        /**
         * 单价
         */
        @ApiModelProperty(value = "单价")
        @NotNull(message = "阶梯价单价不能为空")
        private BigDecimal unitPrice;

        @AssertTrue(message = "阶梯价最小值最大值不能同时为空")
        public Boolean getCheckResult() {
            return minQuantity != null || maxQuantity != null;
        }
    }

    @Data
    public static class GoodsTaste {
        /**
         * 最小下单数量
         */
        @ApiModelProperty(value = "最小下单数量")
        @NotNull(message = "最小下单数量不能为空")
        private Integer minQuantity;
        /**
         * 口味
         */
        @ApiModelProperty(value = "口味")
        @NotEmpty(message = "口味名称不能为空")
        private String tasteName;
        /**
         * 商品颜色
         */
        @ApiModelProperty(value = "商品颜色", example = "#2b2d30")
        @NotEmpty(message = "口味颜色不能为空")
        private String goodsColor;
        /**
         * 产品编号
         */
        @ApiModelProperty(value = "产品编号")
        @NotEmpty(message = "产品编号不能为空")
        private String productNo;
    }

}
